<template>
  <div>
    <el-row>
      <search v-model="form" @submit="getTableList">
        <template slot="item">
          <el-form-item label="表名">
            <el-input v-model="table.query.tableName" placeholder="表名"></el-input>
          </el-form-item>
        </template>
      </search>
      <smallCard :top="10">
        <div slot="content">
          <vxe-table border
                     :data="table.list">
            <vxe-table-column field="tableName" title="表名" ></vxe-table-column>
            <vxe-table-column field="tableComment" title="表描述"></vxe-table-column>
            <vxe-table-column field="createTime" title="创建时间"></vxe-table-column>
            <vxe-table-column field="updateTime" title="更新时间"></vxe-table-column>
            <vxe-table-column title="操作" >
              <template v-slot="{ row }">
                <el-button size="mini" type="success" @click="genCode( row)"
                           v-if="$hasButton('genCode')">代码生成
                </el-button>
              </template>
            </vxe-table-column>
          </vxe-table>
        </div>
      </smallCard>
      <el-dialog
        v-dialogDrag
        :visible.sync="visible"
        title="代码生成"
        center
        width="30%">
        <el-form :model="form" size="mini" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
          <el-form-item label="实体类名" prop="entity" >
            <el-input v-model="form.entity" :placeholder="rules.entity[0].message"></el-input>
          </el-form-item>
          <el-form-item label="模块名" prop="mode" >
            <el-input v-model="form.mode" :placeholder="rules.mode[0].message"></el-input>
          </el-form-item>
        </el-form>
        <span class="dialog-footer" slot="footer" style="text-align: right!important;">
          <el-button size="mini" @click="visible = false">取 消</el-button>
          <el-button size="mini" @click="resetForm('ruleForm')">重置</el-button>
          <el-button size="mini" @click="save" type="primary">保 存</el-button>
        </span>
      </el-dialog>
    </el-row>
  </div>
</template>

<script>
  import search from '@/components/my/TopSerach'
  import smallCard from '_c/my/SmallCard'
  import { SYSTEM } from '@/api'

  export default {
    components: {
      search,
      smallCard
    },
    data() {
      return {
        visible: false,
        form: {table: ''},
        table: {
          query: {},
          list: []
        },
        rules: {
          entity: [
            {required: true, message: '请输入实体类名', trigger: 'blur'}
          ],
          mode: [
            {required: true, message: '请输入模块名', trigger: 'blur'}
          ]
        }
      }
    },
    computed: {},
    mounted() {
      this.getTableList()
    },
    watch: {},
    methods: {
      genCode(row) {
        this.form = {
          table: row.tableName
        }
        this.visible = true
      },
      getTableList() {
        SYSTEM.tableList(this.table.query).then(res => {
          if (res.code === 200) {
            this.table.list = res.data
          } else {
            this.table.list = []
          }
        })
      },
      save() {
        if(this.$validate(this, 'ruleForm')) {
          SYSTEM.genCode(this.form).then(res => {
            if (res.code === 200) {
              this.$message.success(res.message)
            } else {
              this.$message.error(res.message)
            }
            this.visible = false
          })
        }
      },
      resetForm(formName) {
        const table = this.form.table
        this.form = {
          table
        }
      }
    }
  }
</script>
<style lang="scss" scoped>
</style>
